#include <gtest/gtest.h>

#include "helper/sqlite/Transaction.h"
#include "helper/sqlite/MemoryDB.h"

TEST(SqliteTransactionTest, use){
  helper::sqlite::MemoryDB db;
  {
    helper::sqlite::Transaction trans(db);

    ASSERT_TRUE(trans.isInited());
    ASSERT_EQ(std::errc::operation_not_permitted, trans.commit());
    ASSERT_EQ(std::errc::operation_not_permitted, trans.rollback());

    ASSERT_EQ(0, trans.begin().value());
    ASSERT_TRUE(trans.isStarted());
    ASSERT_EQ(0, trans.commit().value());
    ASSERT_TRUE(trans.isCommitted());
  }
  {
    helper::sqlite::Transaction trans(db);

    ASSERT_TRUE(trans.isInited());
    ASSERT_EQ(0, trans.begin().value());
    ASSERT_TRUE(trans.isStarted());
    ASSERT_EQ(0, trans.rollback().value());
    ASSERT_TRUE(trans.isRollbacked());
  }
}

TEST(ScopedTransactionForSqliteTest, use){
  helper::sqlite::MemoryDB db;

  std::unique_ptr<helper::transaction::ITransaction> trans = std::make_unique<helper::sqlite::Transaction>(db);
  helper::transaction::ScopeTransaction st(std::move(trans));  

  ASSERT_EQ(0, st.rollback().value());
}
